热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

C#|栈类

C#|栈类原文:https://www.geeksforgee

C# |栈类

原文:https://www.geeksforgeeks.org/c-sharp-stack-class/

代表一个 后进先出 的集合对象。当您需要后进先出访问项目时,可以使用它。当您在列表中添加一个项目时,它被称为推动项目,当您移除它时,它被称为弹出项目。该类属于 系统。集合 命名空间。

栈类特征:


  • 堆栈的容量是堆栈可以容纳的元素数量。随着元素添加到堆栈中,容量会根据需要通过重新分配自动增加。

  • 如果计数小于堆栈的容量,推送是一个0(1)操作。如果需要增加容量来容纳新元素,Push 将成为 O(n) 操作,其中 n 为 Count。Pop 是一个 O(1) 操作。

  • 堆栈接受 null 作为有效值,并允许重复元素。


构造器

| 构造器 | 描述 |
| Stack() | 初始化堆栈类的新实例,该实例为空并具有默认的初始容量。 |
| 堆栈(ICollection) | 初始化堆栈类的新实例,该实例包含从指定集合复制的元素,并且具有与复制的元素数量相同的初始容量。 |
| 堆栈(Int32) | 初始化堆栈类的新实例,该实例为空,具有指定的初始容量或默认初始容量,以较大者为准。 |

示例:

// C# code to create a Stack
using System;
using System.Collections;
class GFG {
    // Driver code
    public static void Main()
    {
        // Creating a Stack
        Stack myStack = new Stack();
        // Inserting the elements into the Stack
        myStack.Push("1st Element");
        myStack.Push("2nd Element");
        myStack.Push("3rd Element");
        myStack.Push("4th Element");
        myStack.Push("5th Element");
        myStack.Push("6th Element");
        // Displaying the count of elements
        // contained in the Stack
        Console.Write("Total number of elements in the Stack are : ");
        Console.WriteLine(myStack.Count);
        // Displaying the top element of Stack
        // without removing it from the Stack
        Console.WriteLine("Element at the top is : " + myStack.Peek());
        // Displaying the top element of Stack
        // without removing it from the Stack
        Console.WriteLine("Element at the top is : " + myStack.Peek());
        // Displaying the count of elements
        // contained in the Stack
        Console.Write("Total number of elements in the Stack are : ");
        Console.WriteLine(myStack.Count);
    }
}

输出:

Total number of elements in the Stack are : 6
Element at the top is : 6th Element
Element at the top is : 6th Element
Total number of elements in the Stack are : 6


性能

| 财产 | 描述 |
| 计数 | 获取堆栈中包含的元素数量。 |
| 同步 | 获取一个值,该值指示对堆栈的访问是否同步(线程安全)。 |
| 【sync root】 | 获取可用于同步对堆栈的访问的对象。 |

示例:

// C# code to Get the number of
// elements contained in the Stack
using System;
using System.Collections;
class GFG {
    // Driver code
    public static void Main()
    {
        // Creating a Stack
        Stack myStack = new Stack();
        // Inserting the elements into the Stack
        myStack.Push("Chandigarh");
        myStack.Push("Delhi");
        myStack.Push("Noida");
        myStack.Push("Himachal");
        myStack.Push("Punjab");
        myStack.Push("Jammu");
        // Displaying the count of elements
        // contained in the Stack
        Console.Write("Total number of elements in the Stack are : ");
        Console.WriteLine(myStack.Count);
    }
}

输出:

Total number of elements in the Stack are : 6


方法

| 方法 | 描述 |
| 晴() | 从堆栈中移除所有对象。 |
| T1】克隆()T3】 | 创建堆栈的浅拷贝。 |
| 包含(对象) | 确定元素是否在堆栈中。 |
| CopyTo(Array,Int32) | 从指定的数组索引开始,将堆栈复制到现有的一维数组。 |
| 等于(对象) | 确定指定的对象是否等于当前对象。 |
| 【get 分子() | 为堆栈返回一个 IEnumerator。 |
| GetHashCode() | 用作默认哈希函数。 |
| gettype() | 获取当前实例的类型。 |
| MemberWiseCrone() | 创建当前对象的浅拷贝。 |
| 【Peek() | 返回堆栈顶部的对象,而不移除它。 |
| 【pop() | 移除并返回堆栈顶部的对象。 |
| 【推(物) | 在堆栈顶部插入一个对象。 |
| 【同步(叠加) | 返回堆栈的同步(线程安全)包装。 |
| 【toaarray() | 将堆栈复制到新阵列。 |
| ToString() | 返回表示当前对象的字符串。 |

示例:

// C# code to Remove all
// objects from the Stack
using System;
using System.Collections;
class GFG {
    // Driver code
    public static void Main()
    {
        // Creating a Stack 
        Stack myStack = new Stack();
        // Inserting the elements into the Stack
        myStack.Push("1st Element");
        myStack.Push("2nd Element");
        myStack.Push("3rd Element");
        myStack.Push("4th Element");
        myStack.Push("5th Element");
        myStack.Push("6th Element");
        // Displaying the count of elements
        // contained in the Stack before
        // removing all the elements
        Console.Write("Total number of elements in the Stack are : ");
        Console.WriteLine(myStack.Count);
        // Removing all elements from Stack
        myStack.Clear();
        // Displaying the count of elements
        // contained in the Stack after
        // removing all the elements
        Console.Write("Total number of elements in the Stack are : ");
        Console.WriteLine(myStack.Count);
    }
}

Output:

Total number of elements in the Stack are : 6
Total number of elements in the Stack are : 0

示例:

// C# code to Check if a Stack
// contains an element
using System;
using System.Collections;
class GFG {
    // Driver code
    public static void Main()
    {
        // Creating a Stack of strings
        Stack myStack = new Stack();
        // Inserting the elements into the Stack
        myStack.Push("Geeks");
        myStack.Push("Geeks Classes");
        myStack.Push("Noida");
        myStack.Push("Data Structures");
        myStack.Push("GeeksforGeeks");
        // Checking whether the element is
        // present in the Stack or not
        // The function returns True if the
        // element is present in the Stack, else
        // returns False
        Console.WriteLine(myStack.Contains("GeeksforGeeks"));
    }
}

Output:

True

参考:


  • https://docs . Microsoft . com/en-us/dotnet/API/system . collections . stack?视图=netframework-4.7.2


推荐阅读
  • 开发笔记:Java是如何读取和写入浏览器Cookies的
    篇首语:本文由编程笔记#小编为大家整理,主要介绍了Java是如何读取和写入浏览器Cookies的相关的知识,希望对你有一定的参考价值。首先我 ... [详细]
  • Iamtryingtomakeaclassthatwillreadatextfileofnamesintoanarray,thenreturnthatarra ... [详细]
  • 本文介绍了Redis的基础数据结构string的应用场景,并以面试的形式进行问答讲解,帮助读者更好地理解和应用Redis。同时,描述了一位面试者的心理状态和面试官的行为。 ... [详细]
  • Java容器中的compareto方法排序原理解析
    本文从源码解析Java容器中的compareto方法的排序原理,讲解了在使用数组存储数据时的限制以及存储效率的问题。同时提到了Redis的五大数据结构和list、set等知识点,回忆了作者大学时代的Java学习经历。文章以作者做的思维导图作为目录,展示了整个讲解过程。 ... [详细]
  • Spring特性实现接口多类的动态调用详解
    本文详细介绍了如何使用Spring特性实现接口多类的动态调用。通过对Spring IoC容器的基础类BeanFactory和ApplicationContext的介绍,以及getBeansOfType方法的应用,解决了在实际工作中遇到的接口及多个实现类的问题。同时,文章还提到了SPI使用的不便之处,并介绍了借助ApplicationContext实现需求的方法。阅读本文,你将了解到Spring特性的实现原理和实际应用方式。 ... [详细]
  • 1,关于死锁的理解死锁,我们可以简单的理解为是两个线程同时使用同一资源,两个线程又得不到相应的资源而造成永无相互等待的情况。 2,模拟死锁背景介绍:我们创建一个朋友 ... [详细]
  • 本文探讨了C语言中指针的应用与价值,指针在C语言中具有灵活性和可变性,通过指针可以操作系统内存和控制外部I/O端口。文章介绍了指针变量和指针的指向变量的含义和用法,以及判断变量数据类型和指向变量或成员变量的类型的方法。还讨论了指针访问数组元素和下标法数组元素的等价关系,以及指针作为函数参数可以改变主调函数变量的值的特点。此外,文章还提到了指针在动态存储分配、链表创建和相关操作中的应用,以及类成员指针与外部变量的区分方法。通过本文的阐述,读者可以更好地理解和应用C语言中的指针。 ... [详细]
  • 个人学习使用:谨慎参考1Client类importcom.thoughtworks.gauge.Step;importcom.thoughtworks.gauge.T ... [详细]
  • [大整数乘法] java代码实现
    本文介绍了使用java代码实现大整数乘法的过程,同时也涉及到大整数加法和大整数减法的计算方法。通过分治算法来提高计算效率,并对算法的时间复杂度进行了研究。详细代码实现请参考文章链接。 ... [详细]
  • JDK源码学习之HashTable(附带面试题)的学习笔记
    本文介绍了JDK源码学习之HashTable(附带面试题)的学习笔记,包括HashTable的定义、数据类型、与HashMap的关系和区别。文章提供了干货,并附带了其他相关主题的学习笔记。 ... [详细]
  • 在Android开发中,使用Picasso库可以实现对网络图片的等比例缩放。本文介绍了使用Picasso库进行图片缩放的方法,并提供了具体的代码实现。通过获取图片的宽高,计算目标宽度和高度,并创建新图实现等比例缩放。 ... [详细]
  • 向QTextEdit拖放文件的方法及实现步骤
    本文介绍了在使用QTextEdit时如何实现拖放文件的功能,包括相关的方法和实现步骤。通过重写dragEnterEvent和dropEvent函数,并结合QMimeData和QUrl等类,可以轻松实现向QTextEdit拖放文件的功能。详细的代码实现和说明可以参考本文提供的示例代码。 ... [详细]
  • 本文讨论了一个关于cuowu类的问题,作者在使用cuowu类时遇到了错误提示和使用AdjustmentListener的问题。文章提供了16个解决方案,并给出了两个可能导致错误的原因。 ... [详细]
  • Android JSON基础,音视频开发进阶指南目录
    Array里面的对象数据是有序的,json字符串最外层是方括号的,方括号:[]解析jsonArray代码try{json字符串最外层是 ... [详细]
  • 多维数组的使用
    本文介绍了多维数组的概念和使用方法,以及二维数组的特点和操作方式。同时还介绍了如何获取数组的长度。 ... [详细]
author-avatar
cmm雨轩
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有